/* craco.config.js */
/**
 * TODO: 区分环境 —— NODE_ENV
 * - whenDev ☞ process.env.NODE_ENV === 'development'
 * - whenTest ☞ process.env.NODE_ENV === 'test'
 * - whenProd ☞ process.env.NODE_ENV === 'production'
 */
const { when, whenDev, whenProd, whenTest } = require('@craco/craco');
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const SimpleProgressWebpackPlugin = require( 'simple-progress-webpack-plugin' );
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');

// style相关
const TailWindCss = require('tailwindcss');
const AutoPrefixer = require('autoprefixer');

// const FastRefreshCracoPlugin = require('craco-fast-refresh')
const path = require('path');

// 判断编译环境是否为生产
const isProduction = process.env.NODE_ENV === 'production'

if (isProduction) {
  process.env.GENERATE_SOURCEMAP = 'false'
}

const resolve = (dir) => path.join(__dirname, dir);

module.exports = {
  webpack: {
    alias: {
      '@': resolve('src')
    },
    plugins: [
      // 观察打包进度
      new SimpleProgressWebpackPlugin(),
      // 时间转换工具采取day替换moment
      new AntdDayjsWebpackPlugin(),
      ...when(
        !isProduction, () => [
          // 打包分析，生产环境关闭
          // new BundleAnalyzerPlugin()
        ], []
      ),
    ]
  },
  babel: {
    plugins: [
      // AntDesign 按需加载
      ['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }],
      // 用来支持装饰器
      ['@babel/plugin-proposal-decorators', { legacy: true }]
    ],
  },
  plugins: [
    // {
    //   plugin: FastRefreshCracoPlugin
    // }
  ],
  style: {
    postcss: {
      plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
      ],
    },
  },
}
